//
//  LKPieView.m
//  YLHelper
//
//  Created by she1323 on 2018/5/23.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKPieView.h"
#define SpotLabelNumber 5
@implementation LKPieView

+(instancetype)createPieViewWithFrame:(CGRect)frame AndValues:(NSArray *)values AndColors:(NSArray *)colorsArray AndPieNames:(NSArray *)namesArray{
    LKPieView *view = [[LKPieView alloc] initWithFrame:frame];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 50)];
    [view addSubview:topView];
    for (NSInteger i = 0; i < namesArray.count; i++) {
        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(i *(topView.bounds.size.width / SpotLabelNumber), 0, topView.bounds.size.width / SpotLabelNumber, 50)];
        [topView addSubview:subView];
        UIView *spotView = [[UIView alloc] init];
        spotView.backgroundColor = colorsArray[i];
        [subView addSubview:spotView];
        [spotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(10, 10));
            make.centerX.equalTo(subView);
            make.centerY.equalTo(subView.mas_centerY).offset(-8);
        }];
        spotView.layer.cornerRadius = 5;
        spotView.layer.masksToBounds = YES;
        UILabel *titeLabel = [[UILabel alloc] init];
        titeLabel.textAlignment = NSTextAlignmentCenter;
        titeLabel.text = namesArray[i];
        titeLabel.font = [UIFont systemFontOfSize:10];
        titeLabel.textColor = COLOR_HEX(0x3B3B3C);
        [subView addSubview:titeLabel];
        [titeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(subView);
            make.top.equalTo(spotView.mas_bottom).offset(6);
        }];
        
    }
    PieChartView *pieView = [[PieChartView alloc] initWithFrame:CGRectMake(0, 50, view.bounds.size.width, view.bounds.size.height - 50)];
    pieView.chartDescription.enabled = NO;
    [view addSubview:pieView];
    pieView.entryLabelColor = UIColor.whiteColor;
    pieView.entryLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
    NSMutableArray *dataArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < values.count; i++) {
        [dataArra addObject:[[PieChartDataEntry alloc] initWithValue:[values[i] doubleValue] label:namesArray[i] icon:[UIImage imageNamed:@""]]];
        
    }
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:dataArra label:@""];
    dataSet.drawIconsEnabled = YES;
    dataSet.sliceSpace = 0;
    dataSet.iconsOffset = CGPointMake(0, 10);
    dataSet.colors = colorsArray;
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
    
    NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
    pFormatter.numberStyle = NSNumberFormatterPercentStyle;
    pFormatter.maximumFractionDigits = 1;
    pFormatter.multiplier = @1.f;
    pFormatter.percentSymbol = @" %";
    [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
    [data setValueTextColor:UIColor.whiteColor];
    
    pieView.data = data;
    
    //设置图例
    ChartLegend *legend = pieView.legend;
    legend.enabled = NO;
//    legend.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
//    legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
//    legend.orientation = ChartLegendOrientationHorizontal;
////    legend.drawInside = YES;
//    legend.direction = ChartLegendDirectionLeftToRight;
//    legend.form = ChartLegendFormCircle;
//    legend.formSize = 10;
//    legend.font = [UIFont systemFontOfSize:10];
//    legend.textColor = COLOR_HEX(0x595959);
//    legend.yOffset = -10;
    return view;
}




@end
